package com.jtech.together.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jtech.together.R;
import com.jtech.together.manager.UserManager;
import com.jtech.together.model.UserModel;
import com.jtech.together.util.ImageUtils;
import com.jtechlib2.adapter.RecyclerAdapter;
import com.jtechlib2.view.RecyclerHolder;

import cn.bmob.v3.datatype.BmobGeoPoint;

/**
 * 推荐用户列表适配器
 * Created by wuxubaiyang on 2017/5/11.
 */

public class RecommendUserAdapter extends RecyclerAdapter<UserModel> {
    private RecommendUserListener recommendUserListener;

    public RecommendUserAdapter(Context context) {
        super(context);
    }

    /**
     * 设置推荐用户事件监听
     *
     * @param recommendUserListener
     */
    public void setRecommendUserListener(RecommendUserListener recommendUserListener) {
        this.recommendUserListener = recommendUserListener;
    }

    @Override
    protected View createView(LayoutInflater layoutInflater, ViewGroup viewGroup, int viewType) {
        return layoutInflater.inflate(R.layout.view_recommend_user_item, viewGroup, false);
    }

    @Override
    protected void convert(RecyclerHolder holder, final UserModel model, int position) {
        //获取当前用户的位置
        BmobGeoPoint myLocation = UserManager.getCurrentUser().getLocation();
        //设置用户头像
        ImageUtils.showCircleImage(getContext(), model.getAvatar(), holder.getImageView(R.id.imageview_recommend_user_avatar),
                R.drawable.ic_default_avatar, R.drawable.ic_default_avatar);
        //设置用户昵称
        holder.setText(R.id.textview_recommend_user_nickname, model.getNickname());
        //设置用户信息
        holder.setText(R.id.textview_recommend_user_info, model.getInfo());
        //显示距离当前用户的距离（暂时隐藏）
        BmobGeoPoint location = model.getLocation();
        boolean hasLocation = location.getLongitude() != location.getLatitude();
//        boolean hasMyLocation = myLocation.getLongitude() != myLocation.getLatitude();
//        if (hasMyLocation && hasLocation) {
//            double distanceKM = model.getLocation().distanceInKilometersTo(myLocation);
//            holder.setText(R.id.textview_recommend_user_distance, Util.formatDouble(distanceKM) + "km");
//        }
        //如果目标用户没有位置信息则隐藏位置图标
        holder.setViewVisible(R.id.imagebutton_recommend_user_location, hasLocation);
        //设置用户位置点击事件
        holder.setClickListener(R.id.imagebutton_recommend_user_location, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != recommendUserListener) {
                    recommendUserListener.onLocationClick(model);
                }
            }
        });
    }

    /**
     * 推荐用户列表监听
     */
    public interface RecommendUserListener {
        void onLocationClick(UserModel userModel);
    }
}